//
//  WBStatusToolBarView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/21.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

class WBStatusToolBarView: UIView {

    // MARK: -  转发按钮
    var retWeetButton: UIButton?
    // MARK: -  评论按钮
    var commentButton: UIButton?
    // MARK: -  赞按钮
    var unLikeButton: UIButton?
    

    // MARK: -  模型属性
    var status: WBStatusModel? {
        
        didSet {
            
            retWeetButton?.setTitle(getQuantityString((status?.reposts_count)!, title: "转发"), forState: UIControlState.Normal)
            commentButton?.setTitle(getQuantityString((status?.comments_count)!, title: "评论"), forState: UIControlState.Normal)
            unLikeButton?.setTitle(getQuantityString((status?.attitudes_count)!, title: "赞"), forState: UIControlState.Normal)
        }
    }
    
    // MARK: -  获取转发.评论.赞数
    func getQuantityString(count: Int, title: String) -> String {
        
        var countString = ""
        countString.characters.count
        if count <= 0 {
            
            countString = title
        }else if count < 10000 {
            
            countString = "\(count)"
        }else {
            
            let num1 = CGFloat(count / 1000) / 10
            countString = "\(num1)"
            if countString.hasSuffix(".0") {
                
                countString = countString.substringToIndex(countString.endIndex.advancedBy(-2))
                
            }
            
            countString = "\(countString)万"
            
        }
        return countString
    }
    
    // MARK: -  重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    // MARK: -  重写init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置UI
    private func setUpUI() {
        
        
        retWeetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unLikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        let line1 = addLine()
        let line2 = addLine()
        
        retWeetButton!.snp_makeConstraints { (make) -> Void in
            
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retWeetButton!.snp_trailing)
            make.width.equalTo(unLikeButton!)
        }
        
        unLikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(retWeetButton!.snp_trailing)
            make.centerY.equalTo(retWeetButton!)
        }
        
        line2.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(commentButton!)
        }

    }
    
    // MARK: -  添加两条竖线
    func addLine() -> UIImageView {
        
        let lineImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(lineImageView)
        return lineImageView
    }
    
    
    // MARK: -  添加转发.评论.赞的button
    func addChildButton(imageName: String, title: String) -> UIButton {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        addSubview(button)
        
        return button
    }
 
}
